package com.yic.module.mes.dal.mysql.report;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yic.framework.mybatis.core.mapper.BaseMapperX;
import com.yic.module.mes.dal.dataobject.report.ReportDO;
import org.apache.ibatis.annotations.Mapper;
import com.yic.module.mes.controller.admin.report.vo.*;

/**
 * 生产报工 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface ReportMapper extends BaseMapperX<ReportDO> {

    default PageResult<ReportDO> selectPage(ReportPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ReportDO>()
                .eqIfPresent(ReportDO::getMoId, reqVO.getMoId())
                .eqIfPresent(ReportDO::getMoCode, reqVO.getMoCode())
                .eqIfPresent(ReportDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(ReportDO::getBatchNo, reqVO.getBatchNo())
                .eqIfPresent(ReportDO::getItemId, reqVO.getItemId())
                .eqIfPresent(ReportDO::getItemProduceId, reqVO.getItemProduceId())
                .eqIfPresent(ReportDO::getFactoryId, reqVO.getFactoryId())
                .eqIfPresent(ReportDO::getWorkshopId, reqVO.getWorkshopId())
                .eqIfPresent(ReportDO::getCenterId, reqVO.getCenterId())
                .eqIfPresent(ReportDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(ReportDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ReportDO::getCooperate, reqVO.getCooperate())
                .eqIfPresent(ReportDO::getProcessId, reqVO.getProcessId())
                .eqIfPresent(ReportDO::getSerialNo, reqVO.getSerialNo())
                .betweenIfPresent(ReportDO::getApproveTime, reqVO.getApproveTime())
                .eqIfPresent(ReportDO::getApproveUser, reqVO.getApproveUser())
                .eqIfPresent(ReportDO::getApprove, reqVO.getApprove())
                .betweenIfPresent(ReportDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ReportDO::getId));
    }

    default List<ReportDO> selectList(ReportExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ReportDO>()
                .eqIfPresent(ReportDO::getMoId, reqVO.getMoId())
                .eqIfPresent(ReportDO::getMoCode, reqVO.getMoCode())
                .eqIfPresent(ReportDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(ReportDO::getBatchNo, reqVO.getBatchNo())
                .eqIfPresent(ReportDO::getItemId, reqVO.getItemId())
                .eqIfPresent(ReportDO::getItemProduceId, reqVO.getItemProduceId())
                .eqIfPresent(ReportDO::getFactoryId, reqVO.getFactoryId())
                .eqIfPresent(ReportDO::getWorkshopId, reqVO.getWorkshopId())
                .eqIfPresent(ReportDO::getCenterId, reqVO.getCenterId())
                .eqIfPresent(ReportDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(ReportDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ReportDO::getCooperate, reqVO.getCooperate())
                .eqIfPresent(ReportDO::getProcessId, reqVO.getProcessId())
                .eqIfPresent(ReportDO::getSerialNo, reqVO.getSerialNo())
                .betweenIfPresent(ReportDO::getApproveTime, reqVO.getApproveTime())
                .eqIfPresent(ReportDO::getApproveUser, reqVO.getApproveUser())
                .eqIfPresent(ReportDO::getApprove, reqVO.getApprove())
                .betweenIfPresent(ReportDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ReportDO::getId));
    }

    default List<ReportDO> selectByMoId(Long id) {
        return selectList(new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getMoId, id));
    }

    default List<ReportDO> selectByMoRouteId(Long moRouteId) {
        return selectList(new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getMoRouteId, moRouteId));
    }

    default ReportDO selectBySerialNo(Long moId, Long moRouteId, String serialNo) {
        return selectOne(new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getMoId, moId)
                .eq(ReportDO::getMoRouteId, moRouteId)
                .eq(ReportDO::getSerialNo, serialNo));
    }

    default List<ReportDO> selectBySerialNo(String serialNo) {
        return selectList(new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getSerialNo, serialNo));
    }

    default void batchUpdate(List<ReportDO> reportList) {
        updateBatch(reportList);
    }

    default List<ReportDO> selectList(Long factoryId, Long workshopId, LocalDateTime[] timeArr, Integer lastFlag) {
        return selectList(new LambdaQueryWrapperX<ReportDO>()
                .eqIfPresent(ReportDO::getFactoryId, factoryId)
                .eqIfPresent(ReportDO::getWorkshopId, workshopId)
                .eqIfPresent(ReportDO::getLastProcess, lastFlag)
                .eqIfPresent(ReportDO::getErpProduceInId, 0L)
                .betweenIfPresent(ReportDO::getCreateTime, timeArr));
    }

    default int updateByErpInId(ReportDO reportDO, Long erpProduceInId) {
        return update(reportDO, new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getErpProduceInId, erpProduceInId));
    }
}
